﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace KyCommonExtend
{
    public static class EnumExtend
    {
        private static Hashtable enumDesciption = GetDescriptionContainer();
        private static Hashtable GetDescriptionContainer()
        {
            enumDesciption = new Hashtable();
            return enumDesciption;
        }

        /// <summary>
        /// 获取枚举值上的描述内容
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static string ToDescription(this Enum value)
        {
            return ToDescription(value, 0);
        }

        /// <summary>
        /// 获取枚举值上的描述内容
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="index">枚举值根据符号|分割后的具体值</param>
        /// <returns></returns>
        public static string ToDescription(this Enum value, int index)
        {
            string name = value.ToString();
            FieldInfo? field = value.GetType().GetField(name);
            if (field == null) return string.Empty;
            object[] customAttributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (customAttributes == null || customAttributes.Length == 0) return name;
            string[] arr = ((DescriptionAttribute)customAttributes[0]).Description.Split('|');
            if (arr.Length < index + 1) return name;
            return arr[index];
        }
        /// <summary>
        /// 枚举转int列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<int> GetEnumValues<T>() where T : Enum
        {
            return Enum.GetValues(typeof(T)).Cast<int>().ToList();

        }
        /// <summary>
        /// 枚举名称列表
        /// </summary>
        /// <param name="type">枚举类型列表</param>
        /// <returns></returns>
        public static List<string> ToEnumNameStrings(this Type type)
        {
            List<string> strings = new List<string>();
            //循环枚举
            foreach (int item in Enum.GetValues(type))
            {
                string name = Enum.GetName(type, item);
                strings.Add(name);
            }
            return strings;

        }
        /// <summary>
        /// 获取枚举值和描述的字典列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> ToEnumDescriptions<T>(this Type type ) where T : Enum
        {
            var descriptions = new Dictionary<int, string>();
             var fields = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);

            foreach (var field in fields)
            {
                var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
                                                .FirstOrDefault() as DescriptionAttribute;
                var description = descriptionAttribute != null ? descriptionAttribute.Description : field.Name;
                var value = (int)Enum.Parse(type, field.Name);
                descriptions[value] = description;
            }

            return descriptions;
        }
    
}
}
